package com.colin.jipay.alipay_hook;


import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Base64;
import android.view.View;
import android.widget.Button;

import com.alibaba.fastjson.JSON;
import com.colin.jipay.utils.PayHelperUtils;
import com.colin.jipay.utils.StringUtils;

import org.json.JSONArray;
import org.json.JSONObject;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.Timer;
import java.util.TimerTask;

import de.robv.android.xposed.XC_MethodHook;
import de.robv.android.xposed.XC_MethodReplacement;
import de.robv.android.xposed.XposedBridge;
import de.robv.android.xposed.XposedHelpers;

/**
 * 

* @ClassName: AlipayHook

* @Description: TODO(这里用一句话描述这个类的作用)

* @author SuXiaoliang

* @date 2018年6月23日 下午1:25:54

*
 */

public class AlipayHook {

	public static String BILLRECEIVED_ACTION = "com.tools.payhelper.alibillreceived";
	public static String QRCODERECEIVED_ACTION = "com.tools.payhelper.aliqrcodereceived";
	public static String SAVEALIPAYCOOKIE_ACTION = "com.tools.payhelper.savealipaycookie";
	public List<Bundle> bundleList = new ArrayList<>();
	private static Intent envIntent;
	private static Context mContext;
	public static ClassLoader mClassLoader;
	private static boolean doNext = true;
	public String mMark = "";
	public String mRMark = "";
	public String mMoney = "";
	public String mRMoney = "";
	public String mNo = "";
	public String mRNo = "";
	public String mTUserid = "";
	public static String mSignkey="33s44g",//密钥
			mAccount="",//支付宝账号（可空）
			mNotifyurl="";//回调地址

	Timer timer = new Timer();
	TimerTask task = new TimerTask() {
		@Override
		public void run() {
//            refreshParams();
//            PayHelperUtils.sendmsg(mContext,">>>>"+mSignkey+mAccount+mNotifyurl+mUserid);
			if (bundleList.size() == 0 || !doNext)
				return;
			doNext = false;
			envIntent.putExtra("app_id","88886666");
			envIntent.putExtra("mExtras",bundleList.get(0));
			envIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			mContext.startActivity(envIntent);
		}
	};

    public void hook(final ClassLoader classLoader,final Context context) {
		securityCheckHook(classLoader);
		mContext = context;
		mClassLoader = classLoader;

		timer.schedule(task,0,1000);
        try {

            Class<?> insertTradeMessageInfo = XposedHelpers.findClass("com.alipay.android.phone.messageboxstatic.biz.dao.TradeDao", classLoader);
//            XposedBridge.hookAllMethods(insertTradeMessageInfo, "insertMessageInfo", new XC_MethodHook() {
//            	@Override
//            	protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
//            		try {
//            			XposedBridge.log("======支付宝个人账号订单start=========");
//
//            			//更新cookie
//                		Intent cookieBroadCastIntent = new Intent();
//                		String alipaycookie=PayHelperUtils.getCookieStr(classLoader);
//                		cookieBroadCastIntent.putExtra("alipaycookie", alipaycookie);
//                		cookieBroadCastIntent.setAction(SAVEALIPAYCOOKIE_ACTION);
//                        context.sendBroadcast(cookieBroadCastIntent);
//
//            			//获取content字段
////            			String content=(String) XposedHelpers.getObjectField(param.args[0], "content");
////            			XposedBridge.log(content);
//            			//获取全部字段
//            			Object object = param.args[0];
//            			String MessageInfo = (String) XposedHelpers.callMethod(object, "toString");
//            			XposedBridge.log(MessageInfo);
//            			String content=StringUtils.getTextCenter(MessageInfo, "content='", "'");
//            			if(content.contains("二维码收款") || content.contains("收到一笔转账")){
//            				JSONObject jsonObject=new JSONObject(content);
//                			String money=jsonObject.getString("content").replace("￥", "");
//                			String mark=jsonObject.getString("assistMsg2");
//                			String tradeNo=StringUtils.getTextCenter(MessageInfo,"tradeNO=","&");
//                			XposedBridge.log("收到支付宝支付订单："+tradeNo+"=="+money+"=="+mark);
//
//                			Intent broadCastIntent = new Intent();
//                			broadCastIntent.putExtra("bill_no", tradeNo);
//                            broadCastIntent.putExtra("bill_money", money);
//                            broadCastIntent.putExtra("bill_mark", mark);
//                            broadCastIntent.putExtra("bill_type", "alipay_payee");
//                            broadCastIntent.setAction(BILLRECEIVED_ACTION);
//                            context.sendBroadcast(broadCastIntent);
//            			}
//                        XposedBridge.log("======支付宝个人账号订单end=========");
//            		} catch (Exception e) {
//            			XposedBridge.log(e.getMessage());
//            		}
//            		super.beforeHookedMethod(param);
//            	}
//            });

			XposedBridge.hookAllMethods(insertTradeMessageInfo, "insertMessageInfo", new XC_MethodHook() {
				@Override
				protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
					try {
						XposedBridge.log("======支付宝个人账号订单start=========");

						//更新cookie
						Intent cookieBroadCastIntent = new Intent();
						String alipaycookie=PayHelperUtils.getCookieStr(classLoader);
						cookieBroadCastIntent.putExtra("alipaycookie", alipaycookie);
						cookieBroadCastIntent.setAction(SAVEALIPAYCOOKIE_ACTION);
						context.sendBroadcast(cookieBroadCastIntent);

						//获取content字段
//            			String content=(String) XposedHelpers.getObjectField(param.args[0], "content");
//            			XposedBridge.log(content);
						//获取全部字段
						Object object = param.args[0];
						String MessageInfo = (String) XposedHelpers.callMethod(object, "toString");
						XposedBridge.log(MessageInfo);
						String content=StringUtils.getTextCenter(MessageInfo, "extraInfo='", "'");
						if(content.contains("收款到账成功")){
							JSONObject jsonObject=new JSONObject(content);
							String money=jsonObject.getString("money");
							String mark=jsonObject.getJSONArray("content").getJSONObject(1).getString("content");
							String tradeNo=mark;
							XposedBridge.log("收到支付宝支付订单："+tradeNo+"=="+money+"=="+mark);

							Intent broadCastIntent = new Intent();
							broadCastIntent.putExtra("bill_no", tradeNo);
							broadCastIntent.putExtra("bill_money", money);
							broadCastIntent.putExtra("bill_mark", mark);
							broadCastIntent.putExtra("bill_type", "alipay_payee");
							broadCastIntent.setAction(BILLRECEIVED_ACTION);
							context.sendBroadcast(broadCastIntent);
						}else if (content.contains("二维码收款到账")){
							JSONObject jsonObject=new JSONObject(content);
							String money=jsonObject.getString("money");
							String mark=jsonObject.getJSONArray("content").getJSONObject(2).getString("content");
							String tradeNo= StringUtils.getTextCenter(content,"tradeNO=","&");
							XposedBridge.log("收到支付宝支付订单："+tradeNo+"=="+money+"=="+mark);

							Intent broadCastIntent = new Intent();
							broadCastIntent.putExtra("bill_no", tradeNo);
							broadCastIntent.putExtra("bill_money", money);
							broadCastIntent.putExtra("bill_mark", mark);
							broadCastIntent.putExtra("bill_type", "alipay");
							broadCastIntent.setAction(BILLRECEIVED_ACTION);
							context.sendBroadcast(broadCastIntent);
						}
						XposedBridge.log("======支付宝个人账号订单end=========");
					} catch (Exception e) {
						XposedBridge.log(e.getMessage());
					}
					super.beforeHookedMethod(param);
				}
			});

			Class<?> insertTFMessageInfo = XposedHelpers.findClass("com.alipay.android.phone.messageboxstatic.biz.dao.ServiceDao", classLoader);
			XposedBridge.hookAllMethods(insertTFMessageInfo, "insertMessageInfo", new XC_MethodHook() {
				@Override
				protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
					try {
//						XposedBridge.log("======>银行卡到账通知");

						//更新cookie
						Intent cookieBroadCastIntent = new Intent();
						String alipaycookie=PayHelperUtils.getCookieStr(classLoader);
						cookieBroadCastIntent.putExtra("alipaycookie", alipaycookie);
						cookieBroadCastIntent.setAction(SAVEALIPAYCOOKIE_ACTION);
						context.sendBroadcast(cookieBroadCastIntent);

						Object object = param.args[0];
						String MessageInfo = (String) XposedHelpers.callMethod(object, "toString");
						String content=StringUtils.getTextCenter(MessageInfo, "content\":\"", ",");
						XposedBridge.log("===>"+content);
						if (content.contains("银行卡收款通知")){
							String TFDetail = StringUtils.getTextCenter(MessageInfo,"assistMsg1\":","已到账");
							String name = StringUtils.getTextCenter(TFDetail,"\"","通过");
							String money = StringUtils.getTextCenter(TFDetail,"转账","元");
							String cardNo = StringUtils.getTextCenter(TFDetail,"尾号","）");

							XposedBridge.log("===>"+"付款人:"+name);
							XposedBridge.log("===>"+"金额:"+money);
							XposedBridge.log("===>"+"卡号:"+cardNo);

							Intent broadCastIntent = new Intent();
							broadCastIntent.putExtra("bill_no", cardNo);
							broadCastIntent.putExtra("bill_money", money);
							broadCastIntent.putExtra("bill_mark", cardNo);
							broadCastIntent.putExtra("bill_type", "alipaytf");
							broadCastIntent.setAction(BILLRECEIVED_ACTION);
							context.sendBroadcast(broadCastIntent);
						}

//						XposedBridge.log("======支付宝商家服务订单end=========");
					} catch (Exception e) {
						PayHelperUtils.sendmsg(context, e.getMessage());
					}
					super.beforeHookedMethod(param);
				}
			});


			Class<?> insertServiceMessageInfo = XposedHelpers.findClass("com.alipay.android.phone.messageboxstatic.biz.dao.ServiceDao", classLoader);
            XposedBridge.hookAllMethods(insertServiceMessageInfo, "insertMessageInfo", new XC_MethodHook() {
            	@Override
            	protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
            		try {
						XposedBridge.log("======支付宝商家服务订单start=========");
						
						//更新cookie
                		Intent cookieBroadCastIntent = new Intent();
                		String alipaycookie=PayHelperUtils.getCookieStr(classLoader);
                		cookieBroadCastIntent.putExtra("alipaycookie", alipaycookie);
                		cookieBroadCastIntent.setAction(SAVEALIPAYCOOKIE_ACTION);
                        context.sendBroadcast(cookieBroadCastIntent);
						
						Object object = param.args[0];
						String MessageInfo = (String) XposedHelpers.callMethod(object, "toString");
						String content=StringUtils.getTextCenter(MessageInfo, "extraInfo='", "'").replace("\\", "");
						XposedBridge.log(content);
						if(content.contains("店员通")){
							String money=StringUtils.getTextCenter(content, "mainAmount\":\"", "\",\"mainTitle");
							String time=StringUtils.getTextCenter(content, "gmtCreate\":", ",gmtValid");
							String no=PayHelperUtils.getOrderId();
							Intent broadCastIntent = new Intent();
                			broadCastIntent.putExtra("bill_no", no);
                            broadCastIntent.putExtra("bill_money", money);
                            broadCastIntent.putExtra("bill_mark", "");
                            broadCastIntent.putExtra("bill_time", time);
                            broadCastIntent.putExtra("bill_type", "alipay_dy");
                            broadCastIntent.setAction(BILLRECEIVED_ACTION);
                            context.sendBroadcast(broadCastIntent);
						}else if(content.contains("收钱到账") || content.contains("收款到账")){
							PayHelperUtils.getTradeInfo(context,alipaycookie);
						}
						XposedBridge.log("======支付宝商家服务订单end=========");
					} catch (Exception e) {
						PayHelperUtils.sendmsg(context, e.getMessage());
					}
            		super.beforeHookedMethod(param);
            	}
            });
            
            // hook设置金额和备注的onCreate方法，自动填写数据并点击
            XposedHelpers.findAndHookMethod("com.alipay.mobile.payee.ui.PayeeQRSetMoneyActivity", classLoader, "onCreate", Bundle.class, new XC_MethodHook() {
                @Override
                protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                	XposedBridge.log("========支付宝设置金额start=========");
                	
                	//更新cookie
            		Intent cookieBroadCastIntent = new Intent();
            		String alipaycookie=PayHelperUtils.getCookieStr(classLoader);
            		cookieBroadCastIntent.putExtra("alipaycookie", alipaycookie);
            		cookieBroadCastIntent.setAction(SAVEALIPAYCOOKIE_ACTION);
                    context.sendBroadcast(cookieBroadCastIntent);
                	
                    Field jinErField = XposedHelpers.findField(param.thisObject.getClass(), "b");
                    final Object jinErView = jinErField.get(param.thisObject);
                    Field beiZhuField = XposedHelpers.findField(param.thisObject.getClass(), "c");
                    final Object beiZhuView = beiZhuField.get(param.thisObject);
                    Intent intent = ((Activity) param.thisObject).getIntent();
					String mark=intent.getStringExtra("mark");
					String money=intent.getStringExtra("money");
					//设置支付宝金额和备注
                    XposedHelpers.callMethod(jinErView, "setText", money);
                    XposedHelpers.callMethod(beiZhuView, "setText", mark);
                    //点击确认
                    Field quRenField = XposedHelpers.findField(param.thisObject.getClass(), "e");
                    final Button quRenButton = (Button) quRenField.get(param.thisObject);
                    quRenButton.performClick();
                    XposedBridge.log("=========支付宝设置金额end========");
                }
            });

			//消息
			Class<?> chatCallback = XposedHelpers.findClass("com.alipay.mobile.socialchatsdk.chat.data.ChatDataSyncCallback", classLoader);
			Class<?> chatDao = XposedHelpers.findClass("com.alipay.mobile.socialcommonsdk.bizdata.chat.data.ChatMsgDaoOp", classLoader);
			Class<?> chatObj = XposedHelpers.findClass("com.alipay.mobile.socialcommonsdk.bizdata.chat.model.ChatMsgObj", classLoader);
			final Class<?> syncmsg = XposedHelpers.findClass("com.alipay.mobile.rome.longlinkservice.syncmodel.SyncMessage", classLoader);
			final Class<?> msgFac = XposedHelpers.findClass("com.alipay.mobile.socialchatsdk.chat.sender.MessageFactory", classLoader);
			final Class<?> sendMsg = XposedHelpers.findClass("com.alipay.mobile.socialchatsdk.chat.sender.request.BaseChatRequest", classLoader);
			//final Class<?> redEn = XposedHelpers.findClass("com.alipay.mobile.redenvelope.proguard.n.a", classLoader);
			final Class<?> chatP = XposedHelpers.findClass("com.alipay.mobile.chatapp.ui.PersonalChatMsgActivity_", classLoader);
			final Class<?> chatB = XposedHelpers.findClass("com.alipay.mobile.chatapp.ui.ChatMsgBaseActivity", classLoader);
			final Class<?> snsCou = XposedHelpers.findClass("com.alipay.android.phone.discovery.envelope.get.SnsCouponDetailActivity", classLoader);
			final Class<?> giftCrow = XposedHelpers.findClass("com.alipay.giftprod.biz.crowd.gw.result.GiftCrowdDetailResult", classLoader);
			final Class<?> wire = XposedHelpers.findClass("com.squareup.wire.Wire", classLoader);
			final Class<?> msgPModel = XposedHelpers.findClass("com.alipay.mobilechat.core.model.message.MessagePayloadModel", classLoader);
			//final Class<?> A = XposedHelpers.findClass("com.alipay.android.phone.discovery.envelope.ui.util.a",classLoader);
			final Class<?> resvdetail = XposedHelpers.findClass("com.alipay.android.phone.discovery.envelope.received.ReceivedDetailActivity",classLoader);

			XposedHelpers.findAndHookMethod(chatP, "onCreate", Bundle.class, new XC_MethodHook() {
				@Override
				protected void afterHookedMethod(MethodHookParam param) throws Throwable {
					super.afterHookedMethod(param);
					Intent intent = ((Activity)param.thisObject).getIntent();
					Bundle bundle = intent.getExtras();
					Set<String> set = bundle.keySet();
					for (String string : set ){
						XposedBridge.log("key="+string+"--value="+bundle.get(string));
					}

				}
			});

			XposedHelpers.findAndHookMethod(chatCallback, "onReceiveMessage", syncmsg,
					new XC_MethodHook() {
						@Override
						protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
							super.afterHookedMethod(param);
							Object object = param.args[0];
							JSONArray msgArr = new JSONArray(syncmsg.getField("msgData").get(object).toString());
							JSONObject msg1 = msgArr.getJSONObject(0);
							String pl = msg1.getString("pl");
							Object wireIns = XposedHelpers.newInstance(wire, new ArrayList<Class>());
							Object decode_pl = XposedHelpers.callMethod(wireIns, "parseFrom", Base64.decode(pl, 0), msgPModel);
							String decode_pl_str = JSON.toJSONString(decode_pl);
							XposedBridge.log(">>>>>>>"+decode_pl_str);
							com.alibaba.fastjson.JSONObject decode_pl_json = JSON.parseObject(decode_pl_str);
							String biz_type = decode_pl_json.getString("biz_type");
							String content = decode_pl_json.getJSONObject("template_data").getString("m");
							String userid = decode_pl_json.getString("from_u_id");
							String link = decode_pl_json.getString("link")+"#";
//							mTUserid = decode_pl_json.getString("to_u_id");

							boolean universalDetail = true;
							String socialCardCMsgId = decode_pl_json.getString("client_msg_id");
							String target = "groupPre";
							String schemeMode= "portalInside";
							String prevBiz = "chat";
							String bizType = "CROWD_COMMON_CASH";
							String sign = StringUtils.getTextCenter(link,"sign=","#");
							String appId = "88886666";
							boolean REALLY_STARTAPP = true;
							String chatUserType = "1";
							String clientVersion = "10.0.0-5";
							boolean startFromExternal = false;
							String crowdId = StringUtils.getTextCenter(link,"crowdNo=","&");
							String socialCardToUserId = decode_pl_json.getString("to_u_id");
							boolean appClearTop = false;
							boolean REALLY_DOSTARTAPP = true;
							String ap_framework_sceneId = "20000167";

							//自动回复
							if (biz_type.equals("CHAT")){
								//TODO
							}else if (biz_type.equals("GIFTSHARE")){
								//sendMsg(userid,"感谢您的充值！正在自动领取 >>>>>");
								envIntent = new Intent(context,snsCou);
//
								Bundle bundle = new Bundle();
								bundle.putString("chatUserId",userid);
								bundle.putString("socialCardCMsgId",socialCardCMsgId);
								bundle.putBoolean("universalDetail",universalDetail);
								bundle.putString("target",target);
								bundle.putString("schemeMode",schemeMode);
								bundle.putString("prevBiz",prevBiz);
								bundle.putString("bizType",bizType);
								bundle.putString("sign",sign);
								bundle.putString("appId",appId);
								bundle.putBoolean("REALLY_STARTAPP",REALLY_STARTAPP);
								bundle.putString("chatUserType",chatUserType);
								bundle.putString("clientVersion",clientVersion);
								bundle.putBoolean("startFromExternal",startFromExternal);
								bundle.putString("crowdNo",crowdId);
								bundle.putString("socialCardToUserId",socialCardToUserId);
								bundle.putBoolean("appClearTop",appClearTop);
								bundle.putBoolean("REALLY_DOSTARTAPP",REALLY_DOSTARTAPP);
								bundle.putString("ap_framework_sceneId",ap_framework_sceneId);

								bundleList.add(bundle);
								PayHelperUtils.sendmsg(context,bundleList.size()+"");
							}else if (biz_type.contains("COLLET")){
								String pay_link = decode_pl_json.getString("link")+"#";
								String payNo = StringUtils.getTextCenter(pay_link,"tradeNO=","#");

								if(mMoney!=null){
									XposedBridge.log("调用增加数据方法==>支付宝");
									Intent broadCastIntent = new Intent();
									broadCastIntent.putExtra("money", mMoney);
									broadCastIntent.putExtra("mark", mMark);
									broadCastIntent.putExtra("type", "alipay_payee");
									broadCastIntent.putExtra("payurl", payNo);
									broadCastIntent.setAction(QRCODERECEIVED_ACTION);
									context.sendBroadcast(broadCastIntent);
								}
							}

//

						}
					});

			//红包详情
			XposedHelpers.findAndHookMethod(snsCou, "a", giftCrow, boolean.class, new XC_MethodHook() {
				@Override
				protected void afterHookedMethod(MethodHookParam param) throws Throwable {
					super.afterHookedMethod(param);
					String s = JSON.toJSONString(param.args[0]);
					com.alibaba.fastjson.JSONObject jsonObject = JSON.parseObject(s);

					mRMark = jsonObject.getJSONObject("giftCrowdInfo").getString("remark");
//                    mMoney = jsonObject.getJSONObject("giftCrowdInfo").getString("amount");
					mRNo = jsonObject.getJSONObject("giftCrowdInfo").getString("crowdNo");
					XposedBridge.log("方法a>>>>>>"+mRMark+"@@@@"+mRNo);
				}
			});

			XposedHelpers.findAndHookMethod(resvdetail, "a", giftCrow, new XC_MethodHook() {
				@Override
				protected void afterHookedMethod(MethodHookParam param) throws Throwable {
					super.afterHookedMethod(param);
					String det = JSON.toJSONString(param.args[0]);
					com.alibaba.fastjson.JSONObject jsonObject = JSON.parseObject(det);
					XposedBridge.log("det>>>>>>>"+jsonObject);
					mRMoney = jsonObject.getJSONObject("giftCrowdFlowInfo").getString("receiveAmount");

					if (bundleList.size() >0){
						bundleList.remove(0);

						Intent broadCastIntent = new Intent();
						broadCastIntent.putExtra("bill_no", mRNo);
						broadCastIntent.putExtra("bill_money", mRMoney);
						broadCastIntent.putExtra("bill_mark", mRMark);
						broadCastIntent.putExtra("bill_type", "alipay");
						broadCastIntent.setAction(BILLRECEIVED_ACTION);
						context.sendBroadcast(broadCastIntent);

//
					}
					((Activity)param.thisObject).finish();
					doNext = true;
//                    XposedBridge.log(">>>>"+jsonObject);
				}
			});

			//代付
			final Class<?> payee = XposedHelpers.findClass("com.alipay.mobile.payee.ui.PayeeActivityNew",classLoader);

			XposedHelpers.findAndHookMethod(payee, "onCreate", Bundle.class, new XC_MethodHook() {
				@Override
				protected void afterHookedMethod(MethodHookParam param) throws Throwable {
					super.afterHookedMethod(param);

					Object that = param.thisObject;
					Intent intent = ((Activity)param.thisObject).getIntent();
					mMoney = intent.getStringExtra("money");
					mMark = intent.getStringExtra("mark");
					String uid = intent.getStringExtra("uid");
					Class<?> rpcRunnerClass = XposedHelpers.findClass("com.alipay.mobile.beehive.rpc.RpcRunner",classLoader);
					Class<?> bClass = null;
					Class<?> aClass = null;
					Class<?> scr = XposedHelpers.findClass("com.alipay.transferprod.rpc.req.SingleCreateReq",classLoader);
					Class<?>[] innerClasses = payee.getDeclaredClasses();
					for (Class<?> innerClass : innerClasses) {
						if ("b".equals(innerClass.getSimpleName())) {
							bClass = innerClass;
						} else if ("a".equals(innerClass.getSimpleName())){
							aClass = innerClass;
						}
					}
					Constructor<?> bConstr = bClass.getDeclaredConstructor(payee);
					Constructor<?> aConstr = aClass.getDeclaredConstructor(payee);

					bConstr.setAccessible(true);
					aConstr.setAccessible(true);

					Object scrO = XposedHelpers.newInstance(scr);
					XposedHelpers.setObjectField(scrO,"desc",mMark);
					XposedHelpers.setObjectField(scrO,"payAmount",mMoney);
					XposedHelpers.setObjectField(scrO,"userId",uid);
					XposedHelpers.setObjectField(scrO,"userName","colin");
					XposedHelpers.setObjectField(scrO,"billName","个人收款");
					XposedHelpers.setObjectField(scrO,"source","app");

					Object rpcRunner = XposedHelpers.newInstance(rpcRunnerClass, bConstr.newInstance(that), aConstr.newInstance(that));
					XposedHelpers.callMethod(rpcRunner,"start", new Object[]{ new Object[]{scrO} });


					XposedHelpers.callMethod(param.thisObject, "finish");
				}

//                }
			});

			XposedHelpers.findAndHookMethod("com.alipay.mobile.payee.ui.PayeeSuccessActivity", classLoader, "onCreate", new Object[]{Bundle.class, new XC_MethodHook() {
				protected void afterHookedMethod(MethodHookParam param) throws Throwable {
					((Activity) param.thisObject).finish();
				}
			}});

            // hook获得二维码url的回调方法
            XposedHelpers.findAndHookMethod("com.alipay.mobile.payee.ui.PayeeQRSetMoneyActivity", classLoader, "a",
            		XposedHelpers.findClass("com.alipay.transferprod.rpc.result.ConsultSetAmountRes", classLoader), new XC_MethodHook() {
                @Override
                protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                	XposedBridge.log("=========支付宝生成完成start========");
                    Field moneyField = XposedHelpers.findField(param.thisObject.getClass(), "g");
					String money = (String) moneyField.get(param.thisObject);
					
					Field markField = XposedHelpers.findField(param.thisObject.getClass(), "c");
					Object markObject = markField.get(param.thisObject);
					String mark=(String) XposedHelpers.callMethod(markObject, "getUbbStr");
					
					Object consultSetAmountRes = param.args[0];
					Field consultField = XposedHelpers.findField(consultSetAmountRes.getClass(), "qrCodeUrl");
					String payurl = (String) consultField.get(consultSetAmountRes);
					XposedBridge.log(money+"  "+mark+"  "+payurl);
					
					if(money!=null){
						XposedBridge.log("调用增加数据方法==>支付宝");
						Intent broadCastIntent = new Intent();
	                    broadCastIntent.putExtra("money", money);
	                    broadCastIntent.putExtra("mark", mark);
	                    broadCastIntent.putExtra("type", "alipay");
	                    broadCastIntent.putExtra("payurl", payurl);
	                    broadCastIntent.setAction(QRCODERECEIVED_ACTION);
	                    context.sendBroadcast(broadCastIntent);
					}
					XposedBridge.log("=========支付宝生成完成end========");
                }
            });
            
            // hook获取loginid
//            XposedHelpers.findAndHookMethod("com.alipay.mobile.quinox.LauncherActivity", classLoader, "onResume",
//            		 new XC_MethodHook() {
//            	@Override
//            	protected void afterHookedMethod(MethodHookParam param) throws Throwable {
//            		PayHelperUtils.isFirst=true;
//            		String loginid=PayHelperUtils.getAlipayLoginId(classLoader);
//            		PayHelperUtils.sendLoginId(loginid, "alipay", context);
//
//            	}
//            });
        } catch (Error | Exception e) {
        	PayHelperUtils.sendmsg(context, e.getMessage());
        }
    }

     private void securityCheckHook(ClassLoader classLoader) {
        try {
            Class<?> securityCheckClazz = XposedHelpers.findClass("com.alipay.mobile.base.security.CI", classLoader);
            XposedHelpers.findAndHookMethod(securityCheckClazz, "a", String.class, String.class, String.class, new XC_MethodHook() {
                @Override
                protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                    Object object = param.getResult();
                    XposedHelpers.setBooleanField(object, "a", false);
                    param.setResult(object);
                    super.afterHookedMethod(param);
                }
            });

            XposedHelpers.findAndHookMethod(securityCheckClazz, "a", Class.class, String.class, String.class, new XC_MethodReplacement() {
                @Override
                protected Object replaceHookedMethod(MethodHookParam param) throws Throwable {
                    return (byte) 1;
                }
            });
            XposedHelpers.findAndHookMethod(securityCheckClazz, "a", ClassLoader.class, String.class, new XC_MethodReplacement() {
                @Override
                protected Object replaceHookedMethod(MethodHookParam param) throws Throwable {
                    return (byte) 1;
                }
            });
            XposedHelpers.findAndHookMethod(securityCheckClazz, "a", new XC_MethodReplacement() {
                @Override
                protected Object replaceHookedMethod(MethodHookParam param) throws Throwable {
                    return false;
                }
            });

        } catch (Error | Exception e) {
            e.printStackTrace();
        }
    }
}